import { App } from 'vue';
import * as Sentry from '@sentry/vue';
import type { Router } from 'vue-router';
// import { isDevMode } from '../utils/env';

function isDevMode(): boolean {
  return import.meta.env.DEV;
}

const initSentry = async (app: App<Element>, router: Router) => {
  // 开发环境不使用 Sentry
  if (isDevMode()) {
    return;
  }
  // 正则匹配域名获取环境、含有-dev的为开发环境、含有-stg的为测试环境、不含-的为生产环境
  let environment = 'production';
  const host = window.location.host;
  // 开发环境
  const ipReg = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
  if (host.includes('-dev') || host.includes('localhost') || ipReg.test(host)) {
    environment = 'development';
  }
  // 测试环境
  if (host.includes('-stg')) {
    environment = 'staging';
  }
  // 生产环境
  if (!host.includes('-')) {
    environment = 'production';
  }

  await Sentry.init({
    app,
    // 环境
    environment,
    dsn: 'https://75cef083f48faa566ceff7683da0c25b@dev-sentry.mcisaas.com/13',
    integrations: [
      new Sentry.BrowserTracing({
        // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
        tracePropagationTargets: ['https://dc-dev1.mcisaas.com/decision-engine'], //TODO 暂无
        routingInstrumentation: Sentry.vueRouterInstrumentation(router)
      }),
      new Sentry.Replay()
    ],
    // 忽略错误
    ignoreErrors: [
      'ResizeObserver loop limit exceeded',
      'ResizeObserver loop completed with undelivered notifications'
    ],
    // Performance Monitoring
    tracesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!
    // Session Replay
    replaysSessionSampleRate: 0.1, // This sets the sample rate at 10%. You may want to change it to 100% while in development and then sample at a lower rate in production.
    replaysOnErrorSampleRate: 1.0 // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.
  });
};

export async function setupSentry(app: App<Element>, router: Router) {
  await initSentry(app, router);
}
